package com.lzh.excel.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;

/**
 * @author zhehen.lu
 * @date 2025/8/3 14:14
 */
public class EasyExcelDoubleToIntFloorConverter implements Converter<Double> {
    public EasyExcelDoubleToIntFloorConverter() {
    }

    @Override
    public Class supportJavaTypeKey() {
        return Double.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public WriteCellData<String> convertToExcelData(WriteConverterContext<Double> context) throws Exception {
        Double value = (Double)context.getValue();
        if (value == null) {
            return new WriteCellData("0");
        } else {
            int i = (new Double(Math.floor(value))).intValue();
            return new WriteCellData(Integer.toString(i));
        }
    }
}
